package com.zkyc.framework.common.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkyc.framework.common.pojo.User;
import com.zkyc.framework.common.vo.user.JudgeUserVo;
import com.zkyc.framework.common.vo.user.UserSelfRoleListVo;
import com.zkyc.framework.common.vo.website.WebsiteVo;
import org.apache.ibatis.annotations.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * (User)表数据库访问层
 *
 * @since 2022-05-17 15:44:57
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select({
            "<script>",
            " SELECT   " +
                    "  e.website_id,   " +
                    "  f.website_name    " +
                    " FROM( SELECT   " +
                    "  d.website_id    " +
                    " FROM (  " +
                    " SELECT   " +
                    "  b.role_id    " +
                    " FROM  `system_user` a,   " +
                    "   system_user_ref_role b,   " +
                    "   system_role r    " +
                    " WHERE   " +
                    "    b.user_id =  #{userId}     " +
                    " AND r.id = b.role_id    " +
                    " AND r.status = 1 ) c,   " +
                    "   system_website_ref_role d    " +
                    " WHERE   " +
                    "   c.role_id = d.role_id    " +
                    " GROUP BY   " +
                    "   d.website_id ) e,   " +
                    "   web_website f    " +
                    "WHERE   " +
                    "   e.website_id = f.id " +
                    " and f.status=1",
            "</script>"
    })
    List<WebsiteVo> getWebsiteListByUserId(@Param("userId") String userId);


    //用户列表
    @Select({
            "<script>",
            "  SELECT  g.* FROM  system_user g  " +
                    "  WHERE g.id IN  " +
                    " (SELECT  DISTINCT  f.user_id  " +
                    "  FROM (SELECT DISTINCT  " +
                    "  c.website_id   " +
                    "  FROM  " +
                    " system_user_ref_role a,  " +
                    " system_website_ref_role c   " +
                    "  WHERE  " +
                    " a.user_id = #{userId}   " +
                    " AND a.role_id = c.role_id)  d,  " +
                    " system_website_ref_role e,  " +
                    " system_user_ref_role f  " +
                    " WHERE   " +
                    " d.website_id=e.website_id  " +
                    " AND e.role_id=f.role_id) " +
                    " ORDER BY g.gmt_updated DESC",
            "</script>"
    })
    Page<User> getUserListByWebsiteId(@Param("userId") String userId, Page userPage);


    @Select({
            "<script>",
            "SELECT DISTINCT   " +
                    "   b.user_id,   " +
                    "   c.role_type AS isAdmin    " +
                    "FROM   " +
                    "   (   " +
                    "   SELECT   " +
                    "      a.role_id,   " +
                    "      a.user_id    " +
                    "   FROM   " +
                    "      system_user_ref_role a,   " +
                    "      system_website_ref_role w    " +
                    "   WHERE   " +
                    "      a.role_id = w.role_id    " +
                    "      AND w.website_id = #{websiteId}    " +
                    "      AND a.user_id IN (${userIdList})) b,   " +
                    "   system_role c    " +
                    "WHERE   " +
                    "   b.role_id = c.id",
            "</script>"
    })
    List<JudgeUserVo> judgeAdmin(@Param("userIdList") String userIdList, @Param("websiteId") Integer websiteId);


    @Select({
            "<script>",
            " select  f.website_id,f.role_id,g.role_name,k.website_name  from   " +
                    "(SELECT  d.website_id,d.role_id  from     " +
                    "(SELECT DISTINCT   " +
                    " b.website_id    " +
                    "  FROM   " +
                    " system_user_ref_role a,   " +
                    " system_website_ref_role b    " +
                    " WHERE   " +
                    " a.user_id = #{userId}    " +
                    " AND a.role_id = b.role_id)  c, system_website_ref_role d,system_user_ref_role e   " +
                    " where  c.website_id=d.website_id   " +
                    " and  d.role_id=e.role_id  " +
                    " and e.user_id = #{userId} ) f, system_role g, web_website k   " +
                    " where g.id=f.role_id    " +
                    " AND f.website_id=k.id",
            "</script>"
    })
    List<UserSelfRoleListVo> getUserSelfRoleList(@Param("userId") String userId);


    @Select({
            "<script>",
            " SELECT " +
                    " 1  " +
                    "FROM " +
                    " system_role_ref_right a, " +
                    " system_user_ref_role b  " +
                    "WHERE " +
                    " b.user_id =  #{userId}  " +
                    " AND a.role_id = b.role_id  " +
                    " AND a.right_id = 20  " +
                    " LIMIT 1",
            "</script>"
    })
    Integer  judgeRightExist(@Param("userId")String userId);


    @Select({
            "<script>",
            "SELECT" +
                    " 1 " +
                    " FROM" +
                    " system_role a," +
                    " system_user_ref_role b " +
                    " WHERE" +
                    " a.id = b.role_id " +
                    " AND b.user_id =  #{userId} " +
                    " AND a.role_type =0" +
                    " LIMIT 1 ",
            "</script>"
    })
    Integer  judgeIsAdmin(@Param("userId")String userId);

    @Select({
            "<script>",
            "SELECT   " +
                    "  f.*    " +
                    "  FROM   " +
                    "  system_user f,   " +
                    "  (   " +
                    "  SELECT   " +
                    "    c.user_id    " +
                    "  FROM   " +
                    "    system_user_ref_role c,   " +
                    "    system_role d,   " +
                    "    ( SELECT a.role_id FROM system_website_ref_role a WHERE a.website_id =#{websiteId} ) b    " +
                    "  WHERE   " +
                    "    b.role_id = c.role_id    " +
                    "    AND d.role_type = 0    " +
                    "    AND d.id = c.role_id    " +
                    "  ) e    " +
                    " WHERE   " +
                    "  f.id = e.user_id   ",
            "</script>"
    })
    List<User> getCenterUserListByWebsiteId(Integer websiteId);
    
    
  
}

